/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.asl.member;

import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.controller.asl.BaseController;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.Order;
import com.hboxs.asl.entity.OrderItem;
import com.hboxs.asl.entity.ReturnsRecord;
import com.hboxs.asl.service.MemberService;
import com.hboxs.asl.service.OrderItemService;
import com.hboxs.asl.service.OrderService;
import com.hboxs.asl.service.ReturnsRecordService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Create by xxx on 12/06/2016
 * Controller - 退货记录
 */
@Controller("aslReturnsRecordController")
@RequestMapping("/weChat/returnsRecord")
public class ReturnsRecordControl extends BaseController {

    @Resource(name = "memberServiceImpl")
    private MemberService memberService;
    @Resource(name = "orderItemServiceImpl")
    private OrderItemService orderItemService;
    @Resource(name = "returnsRecordServiceImpl")
    private ReturnsRecordService returnsRecordService;
    @Resource(name = "orderServiceImpl")
    private OrderService orderService;

    /**
     * 获取退换货列表
     *
     * @param pageable
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, ModelMap modelMap) {
        try {
            Member member = memberService.getCurrent();

            Page<Order> page = orderService.findPage(member, pageable, 2);
            modelMap.addAttribute("orders", page.getContent());
            modelMap.addAttribute("totalPages", page.getTotalPages());
            modelMap.addAttribute("nextPage", page.getPageNumber() + 1);
            return "/yijing/my-order-return";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/yijing/404";
    }

    /**
     * 进入订单详情页面
     *
     * @param id
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String detail(Long id, ModelMap modelMap) {
        try {
            Order order = orderService.find(id);
            modelMap.addAttribute("order", order);
            modelMap.addAttribute("orderItems", order.getOrderItems());
            return "/yijing/myOrders/receipt-OrderItem";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/yijing/404";
    }

    /**
     * 申请退货
     *
     * @param id 订单项id
     * @return json
     */
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public
    @ResponseBody
    Object submit(Long id, ReturnsRecord.Reason reason, String description) {
        try {
            Member member = memberService.getCurrent();
            OrderItem oi = orderItemService.find(id);
            if (oi == null) {
                return JsonMessage.error("订单项不存在");
            }
            if (oi.getReturnsRecord() != null) {
                return JsonMessage.error("已申请退货");
            }
            returnsRecordService.create(member, reason, description, oi);
            return JsonMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

}